//app网络请求
import axios from 'axios'
import Qs from 'qs'
import store from '../store'
import router from '../router/lazy'
import constant from './constant'
import {
  JSEncrypt
} from "jsencrypt";
import Message from 'ant-design-vue/es/message'
import Modal from 'ant-design-vue/es/modal'
// Vue.prototype.$message = messageM
const BASE_URL = 'http://www.awakenchain.com/';
const BASE_URL_IMG = BASE_URL;
const DEBUG_BASE_URL = constant.namespace

// axios 配置
axios.defaults.timeout = constant.timeout;
axios.defaults.withCredentials = false;
axios.defaults.headers['Access-Control-Allow-Origin'] = "*"
axios.defaults.headers.post["Content-Type"] = "application/json";



// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  let token = localStorage.getItem("token");
  if (token) {
    // config.headers['Access-Token'] = token ;
    // config.headers['Accept'] = 'Content-Type:application/json'
    // config.headers['Accept'] = 'Content-Type: multipart/form-data'
    config.headers['Authorization'] = token;
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  console.log("error", error)
  return Promise.reject(error);
});




// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  //   console.log('response',response);

  sessionStorage.setItem('isNeedLogin', false);
  if (response.data.code == 'NEED_LOGIN') {
    // console.log("sessionStorage.getItem('isNeedLogin')", sessionStorage.getItem('isNeedLogin'))
    // if (sessionStorage.getItem('isNeedLogin') == 'false') {
    //   reLogin()
    // }

    Message.error("登录过期请重新登录!", 3);
    setTimeout(function () {
      router.replace({
        path: '/' // 到登录页重新获取token
      })
    }, 1000);
    //清除SSE请求
    if (window.evtSource) {
      window.evtSource.close();
    }
    localStorage.removeItem('token');
    return false
  }
  if (response.headers.authorization) {
    localStorage.setItem('token', response.headers.authorization)
  }
  axios.defaults.headers.common["Authorization"] =
    response.headers.authorization;
  return response;
}, function (err) {
  // 对响应数据做点什么
  err.message = '连接服务器失败!'
  // alert("网络异常");
  // const hide = Message.loading("网络异常  !", 0);
  // const hide = this.$message.loading('Action in progress..', 0);
  // setTimeout(hide, 1500);
  console.log("error", err)
  return Promise.reject(err);
});




//重新登录弹窗
function reLogin() {
  sessionStorage.setItem('isNeedLogin', true);
  let secondsToGo = 5;
  const modal = Modal.error({
    title: '登录过期请重新登录！',
    content: ` ${secondsToGo} 秒后自动跳转到首页`,
    okText: '现在就去',
    onOk() {
      clearInterval(interval);
      clearTimeout(time);
      modal.destroy();
      router.replace({
        path: '/agent/' // 到登录页重新获取token
      })
    },
  })
  const interval = setInterval(() => {
    console.log(secondsToGo)
    secondsToGo -= 1;
    modal.update({
      content: ` ${secondsToGo} 秒后自动跳转到首页`,
    });
  }, 1000);
  const time = setTimeout(() => {
    clearInterval(interval);
    modal.destroy();
    router.replace({
      path: '/agent/' // 到登录页重新获取token
    })
  }, 5000);
}




function post(url, params, successCallBack, fail) {

  axios.post(url, params)

    .then(function (response) {
      successCallBack(response.data);
    })
    .catch(function (response) {
      console.log(response);
      fail && fail(response)
    })
};

function postFrom(url, params, successCallBack, fail) {
  var config = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
      // 'Content-Type': 'application/json'
    },
  };
  axios.post(url, Qs.stringify(params), config)

    .then(function (response) {

      successCallBack(response.data);

    })
    .catch(function (response) {
      console.log(response);
      fail && fail(response)

    })

};

function getForm(url, params, successCallBack, fail) {
  var config = {
    headers: {
      // 'Content-Type': 'application/x-www-form-urlencoded'

      // 'Content-Type': 'application/json'
      'Content-Type': 'application/xhtml+xml'
    },
  };
  axios.get(url, config)

    .then(function (response) {

      successCallBack(response.data);

    })
    .catch(function (response) {
      console.log(response);
      fail && fail(response)

    })

};

function get(url, params, successCallBack, fail) {

  axios.get(url)

    .then(function (response) {
      successCallBack(response.data);
    })
    .catch(function (response) {
      console.log(response);
      fail && fail(response)
    })

}
export default {
  //登录
  login: function (params, callback) {
    post(DEBUG_BASE_URL + "api/user/login", params, callback);
  },
  // 判断权限
  checkPermission: function(params, callback, fail) {
    get("/agent/auth/function/check/"+params.code,params,callback,fail)
  },
  //注册
  register: function (params, callback) {
    post(DEBUG_BASE_URL + "api/user/register", params, callback);
  },
  // 首页数据
  getHomeData: function (params, callback, fail) {
    get(DEBUG_BASE_URL + "api/push/getData", params, callback, fail);
  },

  //级联选择数据
  getAreaData: function (params, callback, fail) {
    post("/agent/division/list", params, callback, fail);
  },

  //从事行业数据
  getTypeData: function (params, callback, fail) {
    get("/agent/industry/list", params, callback, fail);
  },

  //获取短信验证码
  getcode: function (params, callback, fail) {
    post("/agent/member/send/mobile/check", params, callback, fail);
  },
  //短信验证码验证
  codeCfir: function (params, callback, fail) {
    post("/agent/member/online/register", params, callback, fail);
  },
  // 设置登陆&支付安全信息
  setPassword: function (params, callback, fail) {
    post("/agent/member/reg/password/add", params, callback, fail);
  },
  // 个人客户资料登记
  setPersonData: function (params, callback, fail) {
    post("/agent/customer/add", params, callback, fail);
  },
  // 个人客户银行卡新增
  setPersonBank: function (params, callback, fail) {
    post("/agent/customer/add", params, callback, fail);
  },

  // 单位客户邮箱重新发送
  sendEmailagain: function (params, callback, fail) {
    post("/agent/member/reg/reEmail/act", params, callback, fail);
  },
  // 用户修改时，查询数据
  getExistData: function (params, callback, fail) {
    get("/agent/merchant/fetch/" + params, callback, fail);
  },
  //  单位客户资料图片上传
  setOrgPicData: function (params, callback, fail) {
    post("/agent/merchant/update", params, callback, fail);
  },

  //  单位客户基本信息修改
  OrgDatashow: function (params, callback, fail) {
    post("/agent/customer/org/fetch/baseInfoKey", params, callback, fail);
  },
  //  单位客户图片信息修改
  OrgPicDatashow: function (params, callback, fail) {
    post("/agent/customer/org/fetch/imageInfoKey", params, callback, fail);
  },

  // 单位客户开户行名称
  getBankName: function (params, callback, fail) {
    post("/agent/bank/list", params, callback, fail);
  },

  // 单位客户开户行地址
  getBankArea: function (params, callback, fail) {
    post("/agent/bank/cityList", params, callback, fail);
  },

  // 单位客户 开户行分行
  getBankBranch: function (params, callback, fail) {
    post("/agent/bank/bankList", params, callback, fail);
  },

  // 打款验证
  MoneyConfirm: function (params, callback, fail) {
    post("/agent/customer/org/check/amount/", params, callback, fail);
  },
  Close: function (params, callback, fail) {
    post("/agent/merchant/accessNetwork/closeInput", params, callback, fail)
  },
  // 开通产品选择
  getProduct: function (params, callback, fail) {
    post("/agent/merchant/fetch/product", params, callback, fail)
  },
  // 特约商户业务配置 业务配置的数据提交
  SpeConfig: function (params, callback, fail) {
    post("/agent/merchant/update/fee", params, callback, fail);
  },
  // 特约商户信息填写
  SpeInfoadd: function (params, callback, fail) {
    post("/agent/merchant/accessNetwork/inputMsgMer", params, callback, fail);
  },
  // 特约商户资料上传
  Speupload: function (params, callback, fail) {
    post('/agent/merchant/accessNetwork/inputMsgCard', params, callback, fail);
  },


  // 单位客户登录内容

  // 查询登陆账号信息
  AccountInfo: function (params, callback, fail) {
    post('/agent/safe/reset/select/account/login', params, callback, fail);
  },
  // 首页最近三条交易数据
  transactionList: function (params, callback, fail) {
    post('/agent/transaction/accountDetail/list', params, callback, fail);
  },
  // 发送手机验证码(登陆和支付都调这个接口)
  SendCode: function (params, callback, fail) {
    post('/agent/safe/reset/send/mobile', params, callback, fail);
  },
  // 发送邮箱验证码
  SendEmail: function (params, callback, fail) {
    post('/agent/safe/reset/send/Email', params, callback, fail);
  },
  // 忘记密码登陆校验提交
  ForgetSubmit: function (params, callback, fail) {
    post('/agent/safe/reset/password/submit', params, callback, fail);
  },
  // 忘记密码-重置登陆密码
  ResetloginPaw: function (params, callback, fail) {
    post('/agent/safe/reset/password/login', params, callback, fail);
  },

  // 登陆后-查询支付账号信息
  getUserInfo: function (params, callback, fail) {
    post('/agent/safe/reset/select/account/pay', params, callback, fail);
  },
  // 登陆后-查询支付账号信息
  // getUserInfo1: function (params, callback, fail) {
  //   post('/pospweb/safe/reset/select/account/pay', params, callback, fail);
  // },
  // 登陆后-修改登陆密码
  ChangePaw: function (params, callback, fail) {
    post('/agent/safe/update/password/login', params, callback, fail);
  },
  // 登陆后-修改密保问题
  ChangeSafeotp: function (params, callback, fail) {
    post('/agent/safe/update/safe/otp', params, callback, fail);
  },
  // 登陆后-修改支付密码
  ChangePaypwd: function (params, callback, fail) {
    post('/agent/safe/update/password/pay', params, callback, fail);
  },
  // 登陆后-忘记支付密码提交-邮箱提交
  emailSubmit: function (params, callback, fail) {
    post('/agent/safe/reset/password/submit/email', params, callback, fail);
  },
  // 登陆后-忘记支付密码提交-手机号提交
  mobileSubmit: function (params, callback, fail) {
    post('/agent/safe/reset/password/submit/mobile', params, callback, fail);
  },
  // 登陆后-重置支付密码
  ResetPayPaw: function (params, callback, fail) {
    post('/agent/safe/reset/password/pay', params, callback, fail);
  },
  // 客户交易查询
  coustromOrg: function (params, callback, fail) {
    post('/agent/transaction/transList', params, callback, fail);
  },
  // 分润交易明细查询
  profitQuery: function (params, callback, fail) {
    post('/agent/transaction/profit/query', params, callback, fail);
  },
  // 客户提现明细查询
  coustromWithdraw: function (params, callback, fail) {
    post('/agent/transaction/withdraw', params, callback, fail);
  },
  // // 单位客户交易查询导出excel地址
  // importExcel: function (params, callback, fail) {
  //     post('/agent/transaction/excelList', params, callback, fail);
  // },
  // // 单位客户交易查询下载excel
  // downExcel: function (callback, fail) {
  //     post('/agent/transaction/excelListDownload', callback, fail);
  // },

  // 单位客户代付查询
  coustromPaymentOrg: function (params, callback, fail) {
    post('/agent/transaction/withdraw/api', params, callback, fail);
  },
  // 单位客户费率查询
  coustromRateOrg: function (params, callback, fail) {
    get('/agent/transaction/protocolList', params, callback, fail);
  },
  // 单位客户限额查询
  coustromLimitOrg: function (params, callback, fail) {
    get('/agent/transaction/paymentLimit/' + params, callback, fail);
  },
  // 单位客户账务查询
  coustromAccountOrg: function (params, callback, fail) {
    post('/agent/transaction/accountDetail/list', params, callback, fail);
  },
  // 客户查询状态
  // statusQuery: function (params, callback, fail) {
  //   post('/agent/transaction/selectByOption', params, callback, fail);
  // },
  // 交易状态查询list
  statusQuery: function (params, callback, fail) {
    post('/agent/pulldown/config/fetch', params, callback, fail);
  },

  // 单位客户信息查询
  coustromMessageOrg: function (params, callback, fail) {
    get('/agent/memberHome/myData', params, callback, fail);
  },
  // 单位客户信息验证查询
  coustromupOrg: function (params, callback, fail) {
    get('/agent/customer/org/updateInput', params, callback, fail);
  },
  // 单位客户信息发送验证码查询
  coustromPhoneOrg: function (params, callback, fail) {
    post('/agent/memberHome/myData/mobileCheck', params, callback, fail);
  },
  // 单位客户信息发送验证码验证
  coustromsendPhoneOrg: function (params, callback, fail) {
    post('/agent/memberHome/myData/CheckCode', params, callback, fail);
  },
  // 单位客户一般信息修改
  coustromcommonOrg: function (params, callback, fail) {
    post('/agent/customer/org/update', params, callback, fail);
  },
  // 单位客户重要信息修改
  coustromimplOrg: function (params, callback, fail) {
    post('/agent/customer/org/updateImp', params, callback, fail);
  },
  // 个人客户信息验证查询
  coustromup: function (params, callback, fail) {
    get('/agent/customer/updateInput', params, callback, fail);
  },
  // 个人客户一般信息修改
  coustromcommon: function (params, callback, fail) {
    post('/agent/customer/update', params, callback, fail);
  },
  // 个人客户重要信息修改
  coustromimpl: function (params, callback, fail) {
    post('/agent/customer/updateImp', params, callback, fail);
  },

  // 特约商户上线申请
  proRequest: function (params, callback, fail) {
    post('/agent/production/add', params, callback, fail);
  },
  // 系统消息列表
  systemMessList: function (params, callback, fail) {
    post('/agent/messageInform/selectByMessage', params, callback, fail);
  },
  // 公告消息列表
  messList: function (params, callback, fail) {
    post('/agent/messageInform/selectBySite', params, callback, fail);
  },
  // 查看公告详情
  messDetail: function (params, callback, fail) {
    post('/agent/messageInform/selectManageInform/detail', params, callback, fail);
  },
  // 修改公告状态
  changeMessStatus: function (params, callback, fail) {
    post('/agent/messageInform/upSite', params, callback, fail);
  },
  // 修改消息状态
  changesysMessStatus: function (params, callback, fail) {
    post('/agent/messageInform/upMessage', params, callback, fail);
  },
  // 查询是否提交分账信息
  judeSubmitReceipt: function (params, callback, fail) {
    post('/agent/pay/plan/selectBySubmit', params, callback, fail);
  },
  // 查询客户全称
  checkShowName: function (params, callback, fail) {
    post('/agent/pay/plan/selectByShowName', params, callback, fail);
  },
  // 分账信息录入
  submitSubAccount: function (params, callback, fail) {
    post('/agent/pay/plan/submitSubAccount', params, callback, fail);
  },
  // 分账名单审核查询
  subAccountCheckList: function (params, callback, fail) {
    post('/agent/pay/plan/list', params, callback, fail);
  },
  // 分账名单审核查询详情
  subAccountCheckDetail: function (params, callback, fail) {
    post('/agent/pay/plan/detail', params, callback, fail);
  },
  // 收单分账查询查询
  ReceiptCheckList: function (params, callback, fail) {
    post('/agent/pay/plan/record/list', params, callback, fail);
  },
  // 收单分账查询详情
  ReceiptCheckDetail: function (params, callback, fail) {
    post('/agent/pay/plan/record/detail', params, callback, fail);
  },
  // 分账名单查询
  ReceiptListQuery: function (params, callback, fail) {
    post('/agent/pay/plan/mine', params, callback, fail);
  },
  // ----------------------------------------------------------------------------------------------
  // 查询文件路径
  getFilePath: function (params, callback, fail) {
    get('/agent/site/doc/fetchByCode/' + params, null, callback, fail);
  },

  // 代理商普通商户注册-信息填写
  setOrgData: function (params, callback, fail) {
    post("/agent/merchant/network/add", params, callback, fail);
  },
  // 代理商普通户信息资料更新修改
  updateData: function (params, callback, fail) {
    post("/agent/merchant/network/update/basic", params, callback, fail);
  },
  //  代理商普通信息资料提交保存
  OrgDatasave: function (params, callback, fail) {
    post("/agent/merchant/network/update/image", params, callback, fail);
  },
  //  代理商普通信息终端新增
  terminalConfigSave: function (params, callback, fail) {
    post("/agent/merchant/network/update/config", params, callback, fail);
  },
  // 分期扣款查询
  payInstalmentsQuery: function (params, callback, fail) {
    post("/agent/deduct/list", params, callback, fail);
  },
  // 分期扣款查询详情
  payInstalmentsQueryDetail: function (params, callback, fail) {
    get("/agent/deduct/getDeductById/" + params, null, callback, fail);
  },
  // 合动扣款查询
  getActivityList: function (params, callback, fail) {
    post("/agent/cutOrder/getActivityList", params, callback, fail);
  },
  // 合动扣款查询详情
  getActivityListDetail: function (params, callback, fail) {
    post("/agent/cutOrder/getCutOrderDetail", params, callback, fail);
  },
  // 查询代理商已参与的活动
  getSubList: function (params, callback, fail) {
    post("/agent/subject/activity/subList", params, callback, fail);
  },
  //   查询代理商已参与的活动详情
  getSubListDetail: function (params, callback, fail) {
    get("/agent/subject/activity/subDetail/" + params, null, callback, fail);
  },
  //   查询代理商已参与的活动详情
  getMerchantSubListDetail: function (params, callback, fail) {
    get("/agent/subject/activity/merchant/subDetail/" + params, null, callback, fail);
  },


  //  agent下级参数合作机构查询
  lowerLevelQuery: function (params, callback, fail) {
    get("/agent/subject/activity/subordinate/query", params, callback, fail);
  },

  //  agent下级参数查询
  lowerLevelParamQuery: function (params, callback, fail) {
    post("/agent/subject/activity/subordinate/agent/query", params, callback, fail);
  },

  //   agent激活考核查询
  activationAssessmentQuery: function (params, callback, fail) {
    post("/agent/subject/activity/subordinate/activate/query", params, callback, fail);
  },

  //  agent激活考核
  activationAssessment: function (params, callback, fail) {
    post("/agent/subject/activity/subordinate/activate/config", params, callback, fail);
  },

  //   agent交易量考核查询
  transAssessmentQuery: function (params, callback, fail) {
    post("/agent/subject/activity/subordinate/trans/query", params, callback, fail);
  },

  //  agent交易量考核
  transAssessment: function (params, callback, fail) {
    post("/agent/subject/activity/subordinate/trans/config", params, callback, fail);
  },

  //  agent本级考核查询
  currentLevelQuery: function (params, callback, fail) {
    post("/agent/subject/activity/same/level/query", params, callback, fail);
  },

  //  agent导出本级考核明细(校验是否有数据)
  currentLevelDetail: function (params, callback, fail) {
    post("/agent/subject/activity/get/derive/param", params, callback, fail);
  },


  //  agent导出本级考核明细(跳链接方式导出)
  getDownLevelDetail: function (params, callback, fail) {
    post("/agent/subject/activity/derive/activate/detail", params, callback, fail);
  },

  //  agent查询导出历史记录
  HistoryDetail: function (params, callback, fail) {
    post("/agent/subject/activity/derive/history/record", params, callback, fail);
  },

  //  agent下载前校验是否有文件
  CheckBeforeDownload: function (params, callback, fail) {
    get("/agent/subject/activity/check/download/" + params, null, callback, fail);
  },

  //  agent历史记录下载
  HistoryDetailDownload: function (params, callback, fail) {
    post("/agent/subject/activity/download", params, callback, fail);
  },

  //  agent下级考核查询
  SubordinateAppraisalQuery: function (params, callback, fail) {
    post("/agent/subject/activity/subordinate/assess/query", params, callback, fail);
  },



  // 新建商户自定义活动
  addNewActivity: function (params, callback, fail) {
    post("/agent/subject/activity/new", params, callback, fail);
  },

  // 商户活动日期修改
  dateModify: function (params, callback, fail) {
    post("/agent/subject/activity/modify", params, callback, fail);
  },

  // 商户活动撤销
  revokeActivity: function (params, callback, fail) {
    post("/agent/subject/activity/revoke", params, callback, fail);
  },
  // 商户活动撤销列表
  revokeList: function (params, callback, fail) {
    post("/agent/subject/activity/revoke/list", params, callback, fail);
  },
  // 商户配置历史记录
  configLogList: function (params, callback, fail) {
    post("/agent/subject/activity/merchant/config/log", params, callback, fail);
  },


  // 商户活动撤销记录历史列表
  revokeLogList: function (params, callback, fail) {
    post("/agent/subject/activity/revoke/log/list", params, callback, fail);
  },

  // 商户活动 返现查询
  cashBackQuery: function (params, callback, fail) {
    post("/agent/subject/activity/same/merchant/query", params, callback, fail);
  },

  // 获取风险提示语
  getTips: function (params, callback, fail) {
    get('/agent/tips/get/' + params, null, callback, fail);
  },
  // 登陆发送验证码
  getlogincode: function (params, callback, fail) {
    post("/agent/merchant/information/send/mobile/loginCheck", params, callback, fail)
  },
  // 防伪码信息设置

  // 发送验证码
  sendAntiFakeCode: function (params, callback, fail) {
    post("/agent/homePage/myData/mobileCheck", params, callback, fail)
  },

  // 验证验证码
  checkAntiFakeCode: function (params, callback, fail) {
    post("/agent/homePage/myData/CheckCode", params, callback, fail)
  },

  // 确定修改
  confirmAntiFakeSign: function (params, callback, fail) {
    post("/agent/homePage/set/safe/sign", params, callback, fail)
  },

  fetchPubKey: function (params, callback, fail) {
    get('/agent/member/fetch/memberPubKey/' + params.scope + '/' + params.key, null, callback, fail);
  },
  getEncryptData: function (pubKey, data) {
    var crypt = new JSEncrypt();
    crypt.setKey(pubKey);
    var enc = crypt.encrypt(data);
    return enc;
  }

}
